package cn.yitun.chat.yitunservermaincoresingle.main.thread;

import cn.yitun.chat.yituncommonmessage.client.Client;
import cn.yitun.chat.yituncommonmessage.client.ClientHead;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunserverimapicore.business.user.service.UserService;
import cn.yitun.chat.yitunserverimcore.bridge.main.common.session.SocketSessionStore;
import cn.yitun.chat.yitunserverimcore.general.main.biz.setting.manager.MultipleOnlineStrategyManager;
import cn.yitun.chat.yitunserverimcore.general.main.push.auth.AuthPush;
import cn.yitun.chat.yitunserverimcore.general.main.push.user.PersonalPush;
import cn.yitun.chat.yitunservernetcore.server.bridge.session.SocketSessionHandler;
import cn.yitun.chat.yitunservernetcore.server.general.main.common.session.SocketSessionBox;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author yitun
 * @date 2015年3月10日 上午8:53:57
 */
@Component
public class SocketSessionHandlerImpl implements SocketSessionHandler, SocketSessionStore {

	@Resource
	private SocketSessionBox socketSessionBox;
	@Resource
	private AuthPush authPush;
	@Resource
	private UserService userService;
	@Resource
	private MultipleOnlineStrategyManager multipleOnlineStrategyManager;

	@Override
	public void put(SocketSession socketSession, ClientHead clientHead) {
		Client c = clientHead.getClient();
		String type = c.getType();
		Long key = socketSession.getKey();
		type = null == type || "".equals(type) ? "0" : type;
		socketSession.setTag(type);
		boolean isMultiple = multipleOnlineStrategyManager.isMultiple(type);

		if (!isMultiple) {
			Set<SocketSession> offlineSet = new HashSet<>();
			Set<SocketSession> set = socketSessionBox.getSet(key);
			if (set != null && !set.isEmpty()) {

				for (SocketSession ss : set) {
					if (type.equals(ss.getTag())) {
						offlineSet.add(ss);
					}
				}

				Map<String, Object> client = new HashMap<String, Object>();
				client.put("type", c.getType());
				client.put("version", c.getVersion());
				client.put("onlineTimestamp", System.currentTimeMillis());

				authPush.pushOtherOnline(set, c, true);

				for (SocketSession ss : offlineSet) {
					socketSessionBox.remove(ss);
				}
			}
		}
		socketSessionBox.put(socketSession);
		// if (socketSessionBox.isSingleSession()) {
		// CopyOnWriteArraySet<SocketSession> set = socketSessionBox.remove(key);
		// if (set != null && !set.isEmpty()) {
		// Map<String, Object> client = new HashMap<String, Object>();
		// client.put("clientType", "");
		// client.put("clientVersion", "");
		// client.put("onlineTime", "");
		//
		// personalPush.pushOtherOnline(set, client, true);
		// }
		// }

	}
	@Override
	public boolean hasSession(String key) {
		return socketSessionBox.hasSession(key);
	}

	@Override
	public void removeSession(SocketSession socketSession) {
		socketSessionBox.remove(socketSession);

		Long key = socketSession.getKey();
		if (null != key) {
			userService.removeUser(key);
		}
	}

	@Override
	public boolean hasSession(Long userId) {
		return socketSessionBox.hasSession(userId.toString());
	}
}
